#ifndef FILEMANAGER_HPP
#define	FILEMANAGER_HPP

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

#include "utils.hpp"
#include "directorymanager.hpp"

using namespace std;

typedef pair<int, int> posicio;

class fileManager
{
	public:
		fileManager(char* disk);
		~fileManager();
		
		void crearf(const string &nomf, const int &tipus, const unsigned short &mr, const unsigned short &mcc);
		void delf(const string &nomf);
		void insertr(const string &nomf, const string &key, const string &altres);
		void delr(const string &nomf, const string &key);
		void query(const string &nomf, const int &tipus, const string &key_ini, const string &key_fin);

	private:
        directoryManager* _dirman;
        
		// Estructura de les fulles
		struct fulla {
            unsigned short pesq;
            string key;
            string data;
            unsigned short pdret;
            directoryManager::slot fitxer;
            posicio location;
		};
        
        // -------------------------------------------------------------------------------
        // Funcions que gestionen les fulles
        // -------------------------------------------------------------------------------
        fulla getFulla(const int &pag, const int &pos, int klen, int dlen);
        fulla getFulla(const int &pag, const string &key, const int &klen, const int &dlen);
        void setFulla(const fulla &f);
        posicio existFulla(const string &nomf, const string &key);
        bool isValidFulla(const int &pag, const int &pos);
        unsigned short getFirstFulla(const int &pag);
        void setFirstFulla(const int &pag, const unsigned short &pos);
        unsigned short getLastFulla(const int &pag);
        void setLastFulla(const int &pag, const unsigned short &pos);
        int getFirstFreeSlot(const int &pag, const fulla &f);
};

#endif
